<template>
  <div class="person">
    <h2>一辆{{ car.brand }},价值{{ car.price }}</h2>
    <button @click="changePrice">修改汽车的价格</button>
    <button @click="changeBrand">修改汽车的品牌</button>
    <button @click="changeCar">修改汽车</button>
    <hr>
    <h2>当前求和为：{{ sum }}</h2>
    <button @click="changeSum">点我sum+1</button>
  </div>
</template>

<script setup lang="ts" name="Person">
  import { ref, reactive } from "vue";

  let car = reactive({
    brand: '奔驰',
    price: 100
  })

  let sum = ref(0)


  function changePrice() {
    car.price += 100
  }

  function changeBrand() {
    car.brand = '宝马'
  }
  /* 不能给reactive重新分配对象 ,得用Object.assign*/
  function changeCar() {
    // car = { brand: '雅迪', price: 1 }             //这么写页面不更新
    // car = reactive({ brand: '雅迪', price: 1 })  //这么写页面不更新

    Object.assign(car, { brand: '雅迪', price: 1 }) //可以更新

    /* 如果car是ref定义可以直接修改*/
    // car.value = { brand: '雅迪', price: 1 }    
  }

  function changeSum() {
    sum.value += 1
  }

</script>

<style scoped>
  .person {
    width: 100%;
    background-color: skyblue;
    box-shadow: 0px 1px 2px #ddd;
    border-radius: 10px;
    padding: 20px;
  }

  button {
    margin: 0 5px;
  }

  li {
    font-size: 20px;
  }
</style>
